package org.elephas.persistence;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * Bean factory used to generate the dao instance
 * 
 * @author ivaynberg
 * 
 */
@Component
@Qualifier("dao")
public class DaoFactoryBean implements FactoryBean {

	@Autowired
	private SessionFactory sessionFactory;

	public Class getObjectType() {
		return Dao.class;
	}

	public boolean isSingleton() {
		return true;
	}

	public Object getObject() throws Exception {
		return new AbstractDao() {

			@Override
			protected Session getSession() {
				return sessionFactory.getCurrentSession();
			}

		};
	}

}
